﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDOP.Model;
using CDOP.Utility;

namespace CDOP
{
    public partial class ResetPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            if (string.IsNullOrWhiteSpace(Request.QueryString["id"])) return;
            var id = Helper.Decrypt(Request.QueryString["id"]);
            var user = new Authentication().GetAuthenticationByUserName(id);
            if (user == null)
            {
                lblStatus.Text = "No user found";
                Response.Redirect("Login.aspx");
            }
            else
            {
                Session["UserName"] = id;
            }
        }

        protected void BtnResetClick(object sender, EventArgs e)
        {
            var newPassword = txtPassword.Text;
            if (Session["UserName"] == null) return;
            var userName = Session["UserName"].ToString();
            var status = new Authentication().UpdatePassword(userName, txtPassword.Text);
            if (status)
            {
                lblStatus.Text = "Password changed successfully";
                Response.Redirect("Login.aspx");
            }
            else
            {
                lblStatus.Text = "An error occurred";
            }
        }
    }
}